import ProTable from '@ant-design/pro-table';
import {findByDevise} from '@/services/ant-design-pro/devise';
import {useMount, useRequest} from 'ahooks';
import {useState} from 'react';
import {defaultPagination, paginationTool} from '@/utils/util';
import {findByListObjectDataDocumentSource} from '@/services/ant-design-pro/swagger';
import {Button} from 'antd';
import {findBySelectValueList} from '@/services/ant-design-pro/attribute';

const cloneTbale = () => {
  const valem = {
    london: {
      text: '伦敦',
    },
    'New York': {
      text: '纽约',
    },
  };
  const [config, setConfig] = useState<API.DeviseInfo> ();
  const [pagination, setPagination] = useState<API.Pagination> ( defaultPagination () );
  const {
    run: fetchObjectDataDocumentSource,
    refresh,
    data: dataList,
    loading,
  } = useRequest ( findByListObjectDataDocumentSource, {
    debounceWait: 100,
    manual: true,
  } );
  const fetchFindByDevise = async () => {
    try {
      const data = await findByDevise ( 1 );
      const mdata = data.data;
      const columns = mdata.columns;
      for (let i = 0; i < columns.length; i++) {
        let item = columns[i];
        if (item['valueType'] == 'select') {
          const valem = await fetchfindBySelectValueList ( item['attribute'] );
          console.log ( valem );
          item['valueEnum'] = valem;
          item['filters'] = true;
          item['onFilter'] = true;
          columns[i] = item;
        }
      }
      console.log ( columns );
      mdata.columns = columns;

      setConfig ( mdata );
      fetchObjectDataDocumentSource ( mdata.apiUrl, {...pagination} );
    } catch (error) {
      console.log ( error );
    }
  };
  const fetchfindBySelectValueList = async (key: string) => {
    const data = await findBySelectValueList ( key );
    const mdata = data.data;
    console.log ( mdata );
    return mdata;
  };

  useMount ( () => {
    fetchFindByDevise ();
  } );

  return (
    <ProTable
      onDataSourceChange={(d) => {
        console.log ( 'onDataSourceChange', d );
      }}
      onLoad={(d) => {
        // 数据加载完操作
        console.log ( 'onDataSourceChange', d );
      }}
      dataSource={dataList?.data?.list}
      loading={loading}
      pagination={paginationTool ( pagination, dataList?.data?.total, (data1: API.Pagination) => {
        setPagination ( data1 );
        if (config.apiUrl != '') {
          fetchObjectDataDocumentSource ( config.apiUrl, {...pagination} );
        }
      } )}
      search={config?.search?.show ? config?.search : {}}
      options={config?.options?.show ? config.options : false}
      expandable={
        config?.expandable && {
          expandedRowRender: (record: any) => <p>{record.description}</p>,
        }
      }
      toolBarRender={
        config?.toolBarRender
          ? () => [
            <Button
              key="refresh"
              type="primary"
              onClick={() => {
                refresh ();
              }}
            >
              刷新:{dataList?.data?.total}
            </Button>,
          ]
          : false
      }
      footer={config?.footer ? () => 'Here is footer' : false}
      headerTitle={config?.headerTitle}
      scroll={config?.scroll}
      columns={config?.columns}
    ></ProTable>
  );
};

export default cloneTbale;
